﻿package idv.cjcat.emitter.fields {
	use namespace engine;
	
	import idv.cjcat.emitter.*;
	import idv.cjcat.emitter.ds.*;
	import idv.cjcat.emitter.geom.*;
	
	/**
	 * Radial field.
	 */
	public class RadialField extends Field {
		
		public var x:Number;
		public var y:Number;
		public var strength:Number;
		public var distancePower:Number;
		
		public function RadialField(x:Number = 0, y:Number = 0, strength:Number = 1, distancePower:Number = 0) {
			this.x = x;
			this.y = y;
			this.strength = strength;
			this.distancePower = distancePower;
		}
		
		override public function getMotionData2D(pd:ParticleData):MotionData2D {
			var dx:Number = pd.particle.x - this.x;
			var dy:Number = pd.particle.y - this.y;
			var rs:Number = dx * dx + dy * dy;
			if (rs == 0) return MotionData2DPool.get(0, 0);
			var ang:Number = Math.atan2(pd.particle.x - this.x, pd.particle.y - this.y) + Math.PI / 2;
			rs = Math.pow(Math.sqrt(rs), distancePower);
			
			return MotionData2DPool.get(-Math.cos(ang) * strength * rs, Math.sin(ang) * strength * rs);
		}
		
		
		//XML
		//------------------------------------------------------------------------------------------------
		
		override public function toXML():XML {
			var superXML:XML = super.toXML();
			
			var xml:XML = superXML.field[0];
			xml.@type = FieldType.RADIAL;
			xml.@x = x.toString();
			xml.@y = y.toString();
			xml.@strength = strength.toString();
			xml.@distancePower = distancePower.toString();
			
			return superXML;
		}
		
		override public function parseXML(xml:XML):void {
			super.parseXML(xml);
			
			x = parseFloat(xml.@x);
			y = parseFloat(xml.@y);
			strength = parseFloat(xml.@strength);
			distancePower = parseFloat(xml.@distancePower);
		}
		
		//------------------------------------------------------------------------------------------------
		//end of XML
	}
}